import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";

// https://vite.dev/config/
export default defineConfig({
  base: "./",
  plugins: [vue()],
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "src"), // 配置 @ 指向 src 目录
    },
  },
  server: {
    host: "0.0.0.0", // 设置为 0.0.0.0 可以让局域网内的其他设备访问
    port: 3004,
    // 代理配置，用于重定向请求到其他服务器
    base: "/",
    open: false, // 是否自动弹出浏览器页面
    proxy: {
      "/api": {
        target: "http://127.0.0.1:8000",
        changeOrigin: true,
        ws: true,
        secure: false,
        rewrite: (path) => {
          console.log(`Original Path: ${path}`);
          const newPath = path.replace(/^\/api/, "");
          console.log(`Rewritten Path: ${newPath}`);
          return newPath;
        },
      },
      "/dev-api": {
        target: "http://127.0.0.1:8000",
        changeOrigin: true,
        ws: true,
        secure: false,
        rewrite: (path) => {
          console.log(`Original Path: ${path}`);
          const newPath = path.replace(/^\/dev-api/, "");
          console.log(`Rewritten Path: ${newPath}`);
          return newPath;
        },
      },
    },
  },
});
